package com.lxr.test5002;

import android.os.Bundle;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.lxr.test5002.base.BaseActivity;
import com.lxr.test5002.utils.FileUtils;
import com.white.progressview.HorizontalProgressView;

import java.lang.reflect.Method;
import butterknife.BindView;

/**
 * 类描述:[内存卡测试]
 * 说明:
 * @author: LXR-zsm
 * @Date: 2023/12/18 10:52
 */
public class TFActivity extends BaseActivity {
    @BindView(R.id.tvTip)
    TextView tvTip;
    @BindView(R.id.ivTF)
    ImageView ivTF;
    @BindView(R.id.btn_tf_start)
    Button btnTfTest;
    @BindView(R.id.tv_right_count)
    TextView mTvRightCount;
    @BindView(R.id.tv_wrong_count)
    TextView mTvWrongCount;
    //自定义进度条
    @BindView(R.id.pb_tf_progress)
    HorizontalProgressView mProgressBar;
    private static final String DIR_PATH = "/lxr";
    private static final String FILE_PATH = "/test.txt";
    private boolean stopLoop = false;//停止测试TF卡
    private boolean isTesting;
    private String tfCardPath;
    private int repeatCount = 10;


    public TFActivity() {
        super(R.layout.activity_tf_card);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        btnTfTest.setEnabled(false);
        mProgressBar.setMax(repeatCount);
        if (getTFCardInfo() == 0) {
            tvTip.append(FileUtils.getGB(tfCardPath));
        }
    }

    @Override
    protected void initEvent() {
        btnTfTest.setOnClickListener(v -> testTf());

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLoop = true;
    }

    int rightCount = 0;
    int wrongCount = 0;

    private void testTf() {
        if (isTesting) {
            return;
        }
        isTesting = true;
        btnTfTest.setEnabled(false);
        mProgressBar.setProgress(0);
        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < repeatCount; i++) {
                    if (stopLoop) {
                        return;
                    }
                    if (testTfCard()) {
                        rightCount++;
                    } else {
                        wrongCount++;
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mProgressBar.setProgress(rightCount + wrongCount);
                            mTvRightCount.setText("成功次数： " + rightCount + "次");
                            mTvWrongCount.setText("失败次数： " + wrongCount + "次");
                            if (rightCount + wrongCount == repeatCount) {
                                isTesting = false;
                                btnTfTest.setEnabled(true);
                            }
                        }
                    });
                }
            }
        }).start();
    }

    private boolean testTfCard() {
        return FileUtils.creatFile(tfCardPath + DIR_PATH, FILE_PATH)
                && FileUtils.writeDataToTfCard(tfCardPath + DIR_PATH + FILE_PATH)
                && FileUtils.copyFile(tfCardPath + DIR_PATH + FILE_PATH,
                tfCardPath + DIR_PATH + "/newfile.log")
                && FileUtils.deleteFile(tfCardPath + DIR_PATH);
    }

    private int getTFCardInfo() {
        StorageManager storageManager = (StorageManager) getApplicationContext()
                .getSystemService(STORAGE_SERVICE);
        StorageVolume[] storageVolumes;
        try {
            Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList");
            storageVolumes = (StorageVolume[]) getVolumeList.invoke(storageManager);
            Method getVolumeState = StorageManager.class
                    .getDeclaredMethod("getVolumeState", String.class);
            for (StorageVolume storageVolume : storageVolumes) {
                String desc = storageVolume.getDescription(this);
                if (desc.contains("SD")) {
                    Method getPath = StorageVolume.class.getMethod("getPath");
                    tfCardPath = (String) getPath.invoke(storageVolume);
                    String state = (String) getVolumeState
                            .invoke(storageManager, tfCardPath);
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        tvTip.setText("检测到TF卡，TF卡挂载成功!");
                        ivTF.setBackgroundResource(R.drawable.tf_mounted);
                        btnTfTest.setEnabled(true);
                        return 0;
                    } else {
                        tvTip.setText("检测到TF卡，TF卡挂载失败!");
                        ivTF.setBackgroundResource(R.drawable.tf_unmounted);
                        btnTfTest.setEnabled(false);
                        return -1;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvTip.setText("未检测到TF卡");
        ivTF.setBackgroundResource(R.drawable.tf_unmounted);
        return -2;
    }
}